/*
 * Copyright (c) 2012 Keerthi Ramachandran .
 */
package com.kee.example.runner;

import java.util.List;

import com.kee.example.dao.JPAClassExDao;
import com.kee.example.domain.Address;
import com.kee.example.domain.CreditCard;
import com.kee.example.domain.JPAClassEx;
import com.kee.example.util.HibernateUtil;
import org.hibernate.Session;

/**
 * This class... todo (RK) javadoc
 *
 * @author <a href="mailto:Keerthi.Ramachandran@navis.com">Keerthi Ramachandran</a>, 8/29/12
 * @since 2.4. /2012-08-29
 */
public class JPAClassRunner {
    public static void main(String[] args) {
        JPAClassRunner runner = new JPAClassRunner();
        runner.initData();
        runner.intiCreditCard();
        runner.printData();
    }

    private void intiCreditCard() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        CreditCard creditCard = new CreditCard();
        creditCard.setCcString("CC_STRING");
        session.save(creditCard);
        session.getTransaction().commit();
    }

    private void printData() {
        List<JPAClassEx> exList = new JPAClassExDao().findAll();
        for(JPAClassEx ex: exList){
            System.out.println("ex.getId() = " + ex.getId());
            System.out.println("ex.getLongProp() = " + ex.getLongProp());
            System.out.println("ex.getStringProp() = " + ex.getStringProp());
            System.out.println("ex.getAddress().getCity() = " + ex.getAddress().getCity());
            System.out.println("ex.getAddress().getStreet() = " + ex.getAddress().getStreet());
            System.out.println("ex.getAddress().getZipCode() = " + ex.getAddress().getZipCode());
            System.out.println("ex.getHomeAddress().getCity() = " + ex.getHomeAddress().getCity());
            System.out.println("ex.getHomeAddress().getStreet() = " + ex.getHomeAddress().getStreet());
            System.out.println("ex.getHomeAddress().getZipCode() = " + ex.getHomeAddress().getZipCode());
        }
    }


    private void initData() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        JPAClassEx ex = new JPAClassEx();
        ex.setLongProp(12345L);
        ex.setStringProp("StringProp");
        ex.setAddress(new Address("Street", "zip", "city"));
        ex.setHomeAddress(new Address("homest","homezip","home_city"));
        session.save(ex);
        session.getTransaction().commit();
    }


}
